<?php

include_once 'DataProvider.php';
include_once 'Entity.php';

class DanhMuc extends Entity {

    public $dataProvider;

    public function __construct() {
        $this->dataProvider = new DataProvider();
    }

    public function layDanhSachDanhMuc($trangthai = null) {
        $sql = "select * from `danhmuc`";
        if ($trangthai != null) {
            $sql = $sql . " where danhmuc.trangthai = '$trangthai'";
        }
        $tmp = $this->dataProvider->ExecuteQuery($sql);
        $result = array();
        while ($row = mysql_fetch_assoc($tmp)) {
            $result[] = $row;
        }
        return $result;
    }

    public function xoaDanhMuc($id) {
        $sql = "DELETE FROM `danhmuc` WHERE id = '" . $id . "'";
        return DataProvider::ExecuteQuery($sql);
    }

    public function layDanhMuc($id) {
        $sql = "SELECT * from `danhmuc` WHERE `danhmuc`.id = '$id'";
        return DataProvider::GetOneRow($sql);
    }

    public function capnhapDanhMuc($id, $data) {
        $this->locdulieuCapnhat($data);
        $sql = "UPDATE `danhmuc` 
                SET `danhmuc`.tieude = '{$data['tieude']}', 
                    `danhmuc`.trangthai= '{$data['trangthai']}'
                where `danhmuc`.id = '$id'";
        $result = $this->dataProvider->ExecuteQuery($sql);
        return $result;
    }

    public function themmoiDanhMuc($data) {
        $this->locdulieuCapnhat($data);
        $sql = "insert into `danhmuc` (tieude,trangthai) values ('$data[tieude]','$data[trangthai]')";
        return DataProvider::ExecuteQuery($sql);
    }

    public function layTenDanhMuc($id) {
        $sql = "select tieude from `danhmuc` where id = '" . $id . "'";
        return DataProvider::GetOneRow($sql);
    }

    public function danhmucCombox($trangthai = null) {
        $sql = "select danhmuc.id,danhmuc.tieude from `danhmuc`";
        if ($trangthai != null) {
            $sql = $sql . " where danhmuc.trangthai = '$trangthai'";
        }
        $sql = $sql . " order by danhmuc.tieude";
        $tmp = $this->dataProvider->ExecuteQuery($sql);
        $result = array();
        while ($row = mysql_fetch_assoc($tmp)) {
            $result[$row['id']] = $row['tieude'];
        }
        return $result;
    }
  
}
?>
